package com.itheima.tlias.aspect;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias.utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 统计接口耗时逻辑
 */
@Slf4j
@Aspect
//@Component
public class CountTimeAspect {
    //凡是SpringBoot自动创建的类，都能通过依赖注入，注入进来
    @Resource
    private HttpServletRequest request;


    @Around("execution(* com.itheima.tlias.service.impl.*.*(..))" +
            "&& !execution(* com.itheima.tlias.service.impl.LoginServiceImpl.*(..))")
    public Object count(ProceedingJoinPoint point){
        long start = System.currentTimeMillis();
//        String token = request.getHeader("token");
//
//        if (Objects.nonNull(token)){
//            JSONObject jsonObject = JwtUtil.verifyToken(token);
//            log.info("获取token{}",token);
//            log.info("获取操作人{}",jsonObject.get("username"));
//        }

        //获取参数
        log.info("获取参数{}",point.getArgs());
        //获取类名
        log.info("获取类名{}",point.getTarget());
        //获取方法
        log.info("获取方法{}",point.getSignature().getName());

        //执行父类方法
        try {
            Object proceed = point.proceed();
            long end = System.currentTimeMillis() - start;


            log.info("接口耗时：{}",end);
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

    }
}
